package com.qf.day09;

import java.util.Random;

public class TeamPkImpl implements TeamPk{
    @Override
    public void teamPk(Hero[] teamRed, Hero[] teamBlue) {
        //定义一个数组下标
        int indexRed= 0;
        int indexBlue=0;
        //定义两队血量和攻击力
        int blueHp=teamBlue[indexBlue].getHp();
        int blueAtk=teamBlue[indexBlue].getAtk();
        int redHp=teamRed[indexRed].getHp();
        int redAtk=teamRed[indexRed].getAtk();
        //确定哪个队先攻击
        Random random=new Random();
        int i =random.nextInt(2);
        //随机两个数,0红队先攻击,1蓝队先攻击;
        if(i==0){
            System.out.println("红队先攻");
            //如果两个队第三名队员的血量都大于0就继续打,只要有其中一个队的第三名队员血量小于0,对决就结束
            while (teamBlue[indexBlue].getHp()>0&&teamRed[indexRed].getHp()>0&&indexRed<=2&&indexBlue<=2){
                blueHp-=redAtk;
                System.out.println("蓝队第"+(indexBlue+1)+"名队员血量为:"+blueHp);
                redHp-=blueAtk;
                System.out.println("红队第"+(indexRed+1)+"名队员血量为:"+redHp);
                //如果第index名队员血量小于等于0,index加1
                if(blueHp<=0){
                    System.out.println("蓝队第"+(indexBlue+1)+"名队员阵亡");
                    indexBlue++;
                    blueHp=teamBlue[indexBlue].getHp();
                }
                if(redHp<=0){
                    System.out.println("红队第"+(indexRed+1)+"名队员阵亡");
                    indexRed++;
                    redHp=teamRed[indexRed].getHp();
                }
            }if(teamRed[2].getHp()<=0){
                System.out.println("蓝队获胜");
                return;
            }else {
                System.out.println("红队获胜");
                return;
            }
        }else {
            System.out.println("蓝队先攻");
            //如果两个队第三名队员的血量都大于0就继续打,只要有其中一个队的第三名队员血量小于0,对决就结束
            while (indexRed<=2&&indexBlue<=2){
                redHp-=blueAtk;
                System.out.println("红队第"+(indexRed+1)+"名队员血量为:"+redHp);
                blueHp-=redAtk;
                System.out.println("蓝队第"+(indexBlue+1)+"名队员血量为:"+blueHp);
                if(blueHp<=0){
                    System.out.println("蓝队第"+(indexBlue+1)+"名队员阵亡");
                    indexBlue++;
                    blueHp=teamBlue[indexBlue].getHp();
                }
                if(redHp<=0){
                    System.out.println("红队第"+(indexRed+1)+"名队员阵亡");
                    indexRed++;
                    redHp=teamRed[indexRed].getHp();
                }
            }if(teamRed[2].getHp()<=0){
                System.out.println("蓝队获胜");
                return;
            }else {
                System.out.println("红队获胜");
                return;
            }
        }
    }
}
